//
//  RAPStatusCellRetweetView.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/1/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPStatusCellRetweetView: UIView {
    
    var statusCellRetweetViewBottomConstraint: Constraint?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        addSubview(retweetLabel)
        addSubview(retweetPicturesView)
        
        installConstraints()
    }
    
    private func installConstraints() {
        retweetLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(kStatusCellMargin)
            make.width.lessThanOrEqualTo(self).offset(-2 * kStatusCellMargin)
//            make.bottom.lessThanOrEqualTo(self.snp_bottom).offset(-kStatusCellMargin) // 修改
//            make.bottom.lessThanOrEqualTo(self.snp_bottom).offset(-kStatusCellMargin)
        }
        
        retweetPicturesView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetLabel.snp_bottom).offset(kStatusCellMargin)
            make.leading.equalTo(retweetLabel)

//            make.height.equalTo(150) //
//            make.width.equalTo(self).offset(-2 * kStatusCellMargin) //


        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.statusCellRetweetViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(kStatusCellMargin).constraint
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusCellViewModel: RAPStatusCellViewModel? {
        didSet {
            
            guard let viewModel = statusCellViewModel else {
                return
            }
            retweetLabel.text = viewModel.retweetMessage! // 肯定有值
            
            // 设置原创配图gif
            retweetPicturesView.isGIFs = viewModel.isRetPicGIFs
            
            // 卸载约束
            statusCellRetweetViewBottomConstraint?.uninstall()
            if let picURLs = viewModel.statusModel.retweeted_status?.pic_urls where picURLs.count > 0 {
                // 有配图
                self.retweetPicturesView.hidden = false
                retweetPicturesView.pic_urls = picURLs
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusCellRetweetViewBottomConstraint = make.bottom.equalTo(retweetPicturesView).offset(kStatusCellMargin).constraint
                })
            } else {
                // 无配图
                self.retweetPicturesView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.statusCellRetweetViewBottomConstraint = make.bottom.equalTo(retweetLabel).offset(kStatusCellMargin).constraint
                })
            }
            

        }
    }
    
    lazy var retweetLabel: UILabel = {
        let retweetLabel = UILabel(text: "", fontSize: 12)
        retweetLabel.numberOfLines = 0
        return retweetLabel
    }()

    /**
    转发微博配图
    */
    lazy var retweetPicturesView: RAPStatusCellPicturesView = {
        let retweetPicturesView = RAPStatusCellPicturesView()
        
        return retweetPicturesView
    }()
}
